package com.cskaoyan.controller.admin.profile;

import com.cskaoyan.anno.ValidAnno;
import com.cskaoyan.bean.common.BaseParam;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.admin.profile.PasswordVo;
import com.cskaoyan.bean.vo.admin.profile.UserListVo;
import com.cskaoyan.exception.InvalidDataException;
import com.cskaoyan.service.admin.profile.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: erdonc
 * @date: 2022/9/12 17:38
 * TODO:
 */
@RestController
@RequestMapping("admin/profile")
public class AdminProfileController {
    @Autowired
    ProfileService profileService;


    /**
     * @Author: erdonc
     * @date : 2022/9/12 18:34
     * TODO: 用于后台管理员密码修改
     * 请求 URL: http://182.92.235.201:8083/admin/profile/password
     */
    @ValidAnno
    @RequestMapping("password")
    public BaseRespVo password(@RequestBody @Validated PasswordVo passwordVo, BindingResult bingResult) {
        // 校验参数
        if (bingResult.hasFieldErrors()) {
            FieldError fieldError = bingResult.getFieldError();
            //错误的信息
            String message = fieldError.getDefaultMessage();
            throw new InvalidDataException(message);
        }

        int msg = profileService.updatePassword(passwordVo);

        if (msg < 0) {
            return BaseRespVo.pwdError();
        } else {
            return BaseRespVo.ok(null);
        }
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/12 19:00
     * TODO: 后台通知中心，通知列表
     */
    @RequestMapping("lsnotice")
    public BaseRespVo lsnotice(BaseParam page, String title, String type) {
        UserListVo userListVo = profileService.lsNotice(page, title, type);

        if (userListVo == null) {
            return BaseRespVo.unAuthc();
        }
        return BaseRespVo.ok(userListVo);
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/12 17:41
     * TODO: 不知道这个接口干啥的，反正不写报错
     * 请求网址: http://localhost:8083/admin/profile/nnotice
     */
    @RequestMapping("nnotice")
    public BaseRespVo nnotice() {
        return BaseRespVo.ok(0);
    }


}
